package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {
    public static String url="http://43.138.73.175:8080/login.html";
    public LoginPage(){
        super(url);
    }
    //检查页面是否加载成功
    public void loginPageRight() throws InterruptedException {
        //通过查看页面元素是否存在来检查页面加载是否完成
        driver.findElement(By.cssSelector("body > div > div.container-pic > img"));
        //登录输入框
        driver.findElement(By.cssSelector("body > div > div.login-dialog"));
    }

    //检查登录功能--成功登录
    public void LoginSuc() throws IOException {
        driver.findElement(By.cssSelector("#userName")).sendKeys("admin");
        driver.findElement(By.cssSelector("#password")).sendKeys("admin");
        driver.findElement(By.cssSelector("body > div > div.login-dialog > div:nth-child(4) > button")).click();
        //通过检查列表页的元素来检查点击登录之后是否登录成功
        driver.findElement(By.cssSelector("body > div > h2"));
        //通过页面标题来检查是否登录成功
        String expect=driver.getTitle();
        assert expect.equals("图书列表展示");
        getScreenShot(getClass().getName());
        driver.navigate().back();
    }
    //检查登录功能--登录失败
    public void LoginFail() throws IOException {
        //1.通过clear保证输入框没有文本
        driver.findElement(By.cssSelector("#userName")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        //2.刷新保证输入框没有文本
//        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#userName")).sendKeys("admin1");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("body > div > div.login-dialog > div:nth-child(4) > button")).click();
        //处理弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.accept();
        getScreenShot(getClass().getName());
        assert alertText.equals("账号或密码不正确!");
    }

}
